package com.gking.aiService.service.LogsRecord;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gking.aiService.common.Result;
import com.gking.aiService.entity.dao.PagingQueryLogsRecordDao;
import com.gking.aiService.entity.dto.PagingQuerySyLogsRecordDto;
import com.gking.aiService.entity.SyLogsRecord;
import com.gking.aiService.mapper.SyLogsRecordMapper;
import com.gking.aiService.utils.commonUtil.CustomEncryptorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class LogsRecordServiceImpl extends ServiceImpl<SyLogsRecordMapper, SyLogsRecord> implements LogsRecordService {

    @Autowired
    private SyLogsRecordMapper syLogsRecordMapper;

    /**
     * 添加日志记录
     *
     * @param logsRecord 日志信息
     */
    @Override
    public void addLogsRecord(SyLogsRecord logsRecord) {
        this.save(logsRecord);
    }

    /**
     * 分页获取日志记录列表
     *
     * @param page         分页页码
     * @param size         分页大小
     * @param conditionDto 条件
     * @return s
     */
    @Override
    public Result<Page> getPageLogsRecordList(Integer page, Integer size, PagingQueryLogsRecordDao conditionDto) {
        Page<PagingQuerySyLogsRecordDto> pageInfo = new Page<>(page, size);
        String account = conditionDto.getAccount();
        String nickname = conditionDto.getNickname();
        String userId = conditionDto.getUserId();
        String path = conditionDto.getPath();
        String accessStatus = conditionDto.getAccessStatus();
        String responseStatus = conditionDto.getResponseStatus();
        String ipAddress = conditionDto.getIpAddress();

        IPage<PagingQuerySyLogsRecordDto> logsRecordIPage = syLogsRecordMapper.findLogsRecordByCriteria(
                pageInfo, account, nickname, userId, path, accessStatus, responseStatus, ipAddress);

        List<PagingQuerySyLogsRecordDto> list = new ArrayList<>();
        for (PagingQuerySyLogsRecordDto logsRecord : logsRecordIPage.getRecords()) {
            logsRecord.setRequestBody(CustomEncryptorUtil.decrypt(logsRecord.getRequestBody()));
            list.add(logsRecord);
        }

        Page<PagingQuerySyLogsRecordDto> pageResult = (Page<PagingQuerySyLogsRecordDto>) logsRecordIPage;
        pageResult.setRecords(list);

        return Result.success("分页条件获取日志信息成功！", pageResult);
    }

}
